package service;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/**
 *
 * @author yuyok
 */
public class MailService {

    private Properties props;
    private Session session;

    public MailService() {
            this.props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");


            this.session = Session.getInstance(props,
                    new javax.mail.Authenticator() {

                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication("oop11future", "11future");
                        }
                    });
    }

    public void sendMail(String recipient, String subject, String text) {
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("oop11future@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipient));
            message.setSubject(subject);
            message.setText(text);
            Transport.send(message);
            //System.out.println("Done");
        } catch (MessagingException e) {
            //System.out.println("Fail");
            throw new RuntimeException(e);
        }
    }
}
